/**
 * 
 */
package com.store;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.jedi.BaseObj;
import com.tss.util.DbConn;
import com.tss.util.DbRs;

/**
 * @author wevjoso
 *
 */
public class TasktimevMgr extends BaseObj{
	
	
	
	public List get(String otherSQL,String orderField,int pageNo, int pageSize) {
		clearErr();
		DbRs rs = null;
		DbConn conn = null;
		
		try {
			conn = new DbConn();
			String sqlCount = "select count(t.*) as total,sum(t.worktime) as sumworktime,sum(t.normaltime)  as sumnormaltime,sum(t.acttime) as sumacttime," +
					"sum(t.totdays) as sumtotdays ,sum(t.tripfee) as sumtripfee from tasktimev t where 1=1";
			if (otherSQL != null && !otherSQL.trim().equals(""))
				sqlCount += otherSQL;
			conn.prepare(sqlCount);
			DbRs rsCount = conn.executeQuery();
			int total = getInt(rsCount,0,"total");
			int sumworktime = getInt(rsCount,0,"sumworktime");
			int sumnormaltime = getInt(rsCount,0,"sumnormaltime");
			int sumacttime = getInt(rsCount,0,"sumacttime");
			int sumtotdays = getInt(rsCount,0,"sumtotdays");
			double sumtripfee = getDouble(rsCount,0,"sumtripfee");
			setTotal(total);
			setSumacttime(sumacttime);
			setSumworktime(sumworktime);
			setSumnormaltime(sumnormaltime);
			setSumtripfee(sumtripfee);
			setSumtotdays(sumtotdays);
			
			
			boolean b = total % pageSize == 0;
			int pageCount = b ? (total / pageSize) : (total / pageSize + 1);
			if (pageNo > pageCount) pageNo = pageCount;
			String sql = "select t.*  from tasktimev t where 1=1 ";
			if (otherSQL != null && !otherSQL.trim().equals(""))
				sql += otherSQL;
			if (orderField != null && !orderField.trim().equals(""))
				sql += " order by " + orderField;
			conn.prepare(sql);
			rs = conn.executeQuery(pageNo, pageSize);
		} catch (SQLException ex) {
			ex.printStackTrace();
			setErr(ex.getMessage());
			return new ArrayList();
		} catch (Exception e) {
			e.printStackTrace();
			setErr(e.getMessage());
			return new ArrayList();
		} finally {
			conn.close();
		}
		List r = new ArrayList();
		if (rs == null || rs.size() <= 0)
			return r;
		
		for (int i = 0; i < rs.size(); i++) {
			Tasktimev tasktimev = new Tasktimev();
			tasktimev.setEngineerid(get(rs, i, "engineerid"));
			tasktimev.setEngname(get(rs, i, "engname"));
			tasktimev.setPrjname(get(rs, i, "prjname"));
			tasktimev.setCustomer(get(rs, i, "customer"));
			tasktimev.setSalename(get(rs, i, "salename"));
			tasktimev.setMatterinfo(get(rs, i, "matterinfo"));
			tasktimev.setComid(getInt(rs, i, "comid"));
			tasktimev.setProductdir(get(rs, i, "productdir"));
			tasktimev.setAbegindate(getDate(rs, i, "abegindate"));
			tasktimev.setAenddate(getDate(rs, i, "aenddate"));
			tasktimev.setNormaltime(getInt(rs, i, "normaltime"));
			tasktimev.setActtime(getInt(rs, i, "acttime"));
			tasktimev.setTotdays(getInt(rs, i, "totdays"));
			tasktimev.setTripfee(getDouble(rs, i, "tripfee"));
			tasktimev.setTaskaddress(get(rs, i, "taskaddress"));
			tasktimev.setTriptype(get(rs, i, "triptype"));
			tasktimev.setWorktime(getInt(rs, i, "worktime"));
			
			r.add(tasktimev);
		}
		
		return r;
	}
	
}
